﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SMS.Core
{
    /// <summary>
    /// 短信解释类库
    /// </summary>
    public class SMSInterpreter
    {
        #region PDU码

        #region PDU码参数
        /// <summary>
        /// 生成PDU码类型（TPDU的第一个八位位组）
        /// </summary>
        /// <returns></returns>
        private string GeneratePDUType(STPDUType mPDUType)
        {
            string str = "";
            int temp = 0;
            byte test;

            temp = Convert.ToInt32(mPDUType.ReplyPath);
            str += temp.ToString("D");

            temp = Convert.ToInt32(mPDUType.UserDataHeaderIndicator);
            str += temp.ToString("D");

            temp = Convert.ToInt32(mPDUType.StatusReportRequest);
            str += temp.ToString("D");

            temp = Convert.ToInt32(mPDUType.ValidityPeriodFormat);
            str += temp.ToString("D2");

            temp = Convert.ToInt32(mPDUType.RejectDuplicate);
            str += temp.ToString("D");

            temp = Convert.ToInt32(mPDUType.MessageTypeIndicator);
            str += temp.ToString("D2");

            test = TypeConvert.Str_8ToByte(str);
            str = test.ToString("X2");
            return str;
        }
        /// <summary>
        /// 解析PDU码类型（TPDU的第一个八位位组）
        /// </summary>
        /// <param name="mPDUType"></param>
        /// <returns></returns>
        private STPDUType ParsePDUType(string mPDUType)
        {
            int test = 0;
            STPDUType PDUType = new STPDUType();
            byte a = TypeConvert.Str_2toByte(mPDUType);
            string str = TypeConvert.ByteToBit(a);

            test = Convert.ToInt32(str.Substring(0, 1));
            PDUType.ReplyPath = (EReplyPath)test;

            test = Convert.ToInt32(str.Substring(1, 1));
            PDUType.UserDataHeaderIndicator = (EUserDataHeaderIndicator)test;

            test = Convert.ToInt32(str.Substring(2, 1));
            PDUType.StatusReportIndication = (EStatusReportIndication)test;

            test = Convert.ToInt32(str.Substring(5, 1));
            PDUType.MoreMessagestoSend = (EMoreMessagestoSend)test;

            test = Convert.ToInt32(str.Substring(6, 2));
            PDUType.MessageTypeIndicator = (EMessageTypeIndicator)test;

            return PDUType;
        }
        /// <summary>
        /// 生成地址类型
        /// </summary>
        private string GenerateAdressType(STAddressType mAddressType)
        {
            string str = "";
            int temp = 0;
            byte test;

            temp = Convert.ToInt32(mAddressType.TypeofNumber);
            str += temp.ToString("D4");

            temp = Convert.ToInt32(mAddressType.NumberingPlanIdentification);
            str += temp.ToString("D4");

            test = TypeConvert.Str_8ToByte(str);
            str = test.ToString("X2");
            return str;
        }
        /// <summary>
        /// 解析地址类型
        /// </summary>
        /// <param name="mAddressType"></param>
        /// <returns></returns>
        private STAddressType ParseAdressType(string mAddressType)
        {
            STAddressType AddressType = new STAddressType();
            int test = 0;
            byte a = TypeConvert.Str_2toByte(mAddressType);
            string str = TypeConvert.ByteToBit(a);

            test = Convert.ToInt32(str.Substring(0, 4));
            AddressType.TypeofNumber = (ETypeofNumber)test;

            test = Convert.ToInt32(str.Substring(4));
            AddressType.NumberingPlanIdentification = (ENumberingPlanIdentification)test;

            return AddressType;


        }
        /// <summary>
        /// 生成数据编码方式(DataCoding-Scheme)
        /// </summary>
        private string GenerateCodeScheme(STDataCodingScheme mDataCodingScheme)
        {
            string str = "";
            int temp = 0;
            byte test;

            temp = Convert.ToInt32(mDataCodingScheme.Default);
            str += temp.ToString("D2");

            temp = Convert.ToInt32(mDataCodingScheme.Compression);
            str += temp.ToString("D");

            temp = Convert.ToInt32(mDataCodingScheme.MessageType);
            str += temp.ToString("D");

            temp = Convert.ToInt32(mDataCodingScheme.CodeScheme);
            str += temp.ToString("D2");

            temp = Convert.ToInt32(mDataCodingScheme.SMSType);
            str += temp.ToString("D2");

            test = TypeConvert.Str_8ToByte(str);
            str = test.ToString("X2");
            return str;
        }
        /// <summary>
        /// 解析数据编码方式(DataCoding-Scheme)
        /// </summary>
        /// <param name="mDataCodingScheme"></param>
        /// <returns></returns>
        private STDataCodingScheme ParseCodeScheme(string mDataCodingScheme)
        {
            STDataCodingScheme DataCodingScheme = new STDataCodingScheme();
            int test = 0;
            byte a = TypeConvert.Str_2toByte(mDataCodingScheme);
            string str = TypeConvert.ByteToBit(a);

            test = Convert.ToInt32(str.Substring(0, 2));
            DataCodingScheme.Default = (EDefault)test;

            test = Convert.ToInt32(str.Substring(2, 1));
            DataCodingScheme.Compression = (ECompression)test;

            test = Convert.ToInt32(str.Substring(3, 1));
            DataCodingScheme.MessageType = (EMessageType)test;

            test = Convert.ToInt32(str.Substring(4, 2));
            DataCodingScheme.CodeScheme = (ECodeScheme)test;

            test = Convert.ToInt32(str.Substring(6));
            DataCodingScheme.SMSType = (ESMSType)test;

            return DataCodingScheme;

        }
        /// <summary>
        /// 生成PDU码
        /// </summary>
        /// <returns></returns>
        public STPDU GeneratePDU(STPDUParam mPDU)
        {
            STPDU PDU;
            int lenPDU = 0;//PDU长度；15+PDU中短信内容字节数
            string str = "";//缓冲
            string SRPDU = "";//PDU码
            int temp = 0;
            int len = 0;

            try
            {
                //服务中心地址
                if (mPDU.ISSCA == EISSCA.NO)
                {
                    SRPDU += "00";
                }
                else
                {
                    str = GenerateAdressType(mPDU.SCAType);
                    str += InvertPhone(mPDU.SCAdress);
                    temp = str.Length / 2;
                    SRPDU += temp.ToString("X2");
                    SRPDU += str;
                }

                //PDU类型
                str = GeneratePDUType(mPDU.PDUType);
                SRPDU += str;

                //消息参考值
                temp = Convert.ToInt32(mPDU.MR);
                str = temp.ToString("D2");
                SRPDU += str;

                //接收方电话号码
                str = GenerateAdressType(mPDU.DAType);
                str += InvertPhone(mPDU.DAdress);
                len = mPDU.DAdress.Length + 2;
                SRPDU += len.ToString("X2");
                SRPDU += str;

                //协议标识
                temp = Convert.ToInt32(mPDU.ProtocolIdentifier);
                str = temp.ToString("D2");
                SRPDU += str;

                //编码方式
                str = GenerateCodeScheme(mPDU.DataCodingScheme);
                SRPDU += str;

                //有效期
                if (mPDU.PDUType.ValidityPeriodFormat == EValidityPeriodFormat.integ)
                {
                    temp = Convert.ToInt32(mPDU.SRValidityPeriod);
                    str = temp.ToString("X2");
                    SRPDU += str;
                }
                else if (mPDU.PDUType.ValidityPeriodFormat == EValidityPeriodFormat.semi_octet)
                {
                    str = mPDU.DTValidityPeriod.ToString("yyMMddHHmmsszz");
                    str = str.Remove(12, 1);
                    str = InvertPhone(str);
                    SRPDU += str;
                }

                //短信内容长度
                if (mPDU.DataCodingScheme.CodeScheme == ECodeScheme.Unicode)
                {
                    len = mPDU.UserData.Length * 2;
                    lenPDU = len + 15;
                    str = len.ToString("X2");
                    SRPDU += str;
                    str = ConvertToUnicode(mPDU.UserData);
                    SRPDU += str;

                }
                else if (mPDU.DataCodingScheme.CodeScheme == ECodeScheme.Sbit)
                {
                    str = ConvertTo7bit(mPDU.UserData);
                    len = mPDU.UserData.Length;
                    SRPDU += len.ToString("X2");
                    SRPDU += str;
                    lenPDU = str.Length / 2 + 15;
                }

                //短信内容
                PDU.strPDU = SRPDU;
                PDU.lenPDU = lenPDU;
                return PDU;
            }
            catch (SystemException er)
            {
                PDU.strPDU = "ERROR:" + er.Message;
                PDU.lenPDU = 0;
                return PDU;
            }
        }
        /// <summary>
        /// 将从模块中通过AT+CMGL接收到的短信解析
        /// </summary>
        /// <param name="Recv"></param>
        /// <returns></returns>
        public STPDUParam ParsePDU_CMGL(string Recv)
        {
            //获取AT+CMGL列出的短信
            int len;
            int invel = 0;
            string temp = "";
            int test = 0;
            string index = "";//短信ID
            STPDUParam mPDU = new STPDUParam();
            len = Recv.IndexOf("+CMGL:");

            while (len != 0)
            {
                //mPDU = new STPDUParam();
                #region 解析CMGL或者CMGR命令返回
                #region AT+CMGL=2的回传信息
                //AT ＋ CMGL=2 { 读未发短信息 } 
                //＋ CMGL:（空格） 1,2,,24 {1 表示短信ID， 2 表示未发信息， 24 表示信息总容量 }（回车，换行） pdu
                //0891683108200505F0  11  0B813106656930F1  0000  40015280353500 04 D4F29C0E （回车换行回车换行）
                //OK （回车换行）
                #endregion

                #region 解析短信参数信息（如ID，状态）
                //跳过+CMGL:           
                len += 6;

                //跳过空格
                len += 1;

                //取消息ID（index）
                index = Recv.Substring(len, 1);


                //跳过短信参数信息（如ID，状态）
                len = Recv.IndexOf("\r\n");
                //跳过回车换行
                len += 2;
                #endregion

                #region 解析PDU码
                #region PDU码中心服务地址
                //长度
                temp = Recv.Substring(len, 2);
                invel = TypeConvert.Str_2toInt(temp);
                len += 2;

                if (invel > 0)
                {
                    //地址类型
                    temp = Recv.Substring(len, 2);
                    mPDU.SCAType = ParseAdressType(temp);
                    len += 2;
                    //中心地址
                    test = invel * 2 - 2;
                    temp = Recv.Substring(len, test);
                    mPDU.SCAdress = UnInvertStr(temp);
                    len += test;
                }
                #endregion

                #region PDU类型
                temp = Recv.Substring(len, 2);
                mPDU.PDUType = ParsePDUType(temp);
                len += 2;
                #endregion

                #region 发送方地址
                //长度
                temp = Recv.Substring(len, 2);
                invel = TypeConvert.Str_2toInt(temp);
                //如果长度为奇数则加1
                if (invel % 2 == 1)
                {
                    invel += 1;
                }
                len += 2;

                if (invel > 0)
                {
                    //地址类型
                    temp = Recv.Substring(len, 2);
                    mPDU.OAType = ParseAdressType(temp);
                    len += 2;
                    //中心地址
                    temp = Recv.Substring(len, invel);
                    mPDU.OAdress = UnInvertStr(temp);
                    len += test;
                }
                #endregion

                #region 协议标识
                temp = Recv.Substring(len, 2);
                test = TypeConvert.Str_2toInt(temp);
                mPDU.ProtocolIdentifier = (EProtocolIdentifier)test;
                len += 2;
                #endregion

                #region 编码方式
                temp = Recv.Substring(len, 2);
                mPDU.DataCodingScheme = ParseCodeScheme(temp);
                len += 2;
                #endregion

                #region 服务中心收到短信的时间
                temp = Recv.Substring(len, 12);
                temp = UnInvertStr(temp);
                temp = temp.Substring(0, 2) + "/" + temp.Substring(2, 2) + "/"
                     + temp.Substring(4, 2) + " " + temp.Substring(6, 2) + ":"
                     + temp.Substring(8, 2) + ":" + temp.Substring(10, 2);
                mPDU.SCTS = Convert.ToDateTime(temp);
                len += 14;
                #endregion

                #region 短信内容
                //长度
                temp = Recv.Substring(len, 2);
                invel = TypeConvert.Str_2toInt(temp);
                len += 2;
                //短信内容
                test = invel * 2;
                temp = Recv.Substring(len, test);
                //解析短息内容
                if (mPDU.DataCodingScheme.CodeScheme == ECodeScheme.Unicode)
                {
                    mPDU.UserData = ConvertFromUnicode(temp);
                }
                Recv = Recv.Substring(len);
                #endregion
                #endregion
                #endregion
            }
            return mPDU;
        }
        /// <summary>
        /// 将从模块中通过命令AT+CMGR接收到的短信解析，并以【STPDUParam】类型作为返回
        /// </summary>
        /// <param name="Recv"></param>
        /// <returns></returns>
        public STPDUParam ParsePDU_CMGR(string Recv)
        {
            //获取AT+CMGL列出的短信
            int len;
            int invel = 0;
            string temp = "";
            int test = 0;
            string state = "";//短信状态
            STPDUParam mPDU = new STPDUParam();
            len = Recv.IndexOf("+CMGR:");

            if (len != 0)
            {
                #region 解析CMGR命令返回
                #region AT+CMGL=2的回传信息
                //AT ＋ CMGR=2 { 读未发短信息 } 
                //＋ CMGL:（空格） 2,,24 {2 表示未发信息， 24 表示短信长度 }（回车，换行） pdu
                //0891683108200505F0  11  0B813106656930F1  0000  40015280353500 04 D4F29C0E （回车换行回车换行）
                //OK （回车换行）
                #endregion

                #region 解析短信参数信息（如ID，状态）
                //跳过+CMGL:           
                len += 6;

                //跳过空格
                len += 1;

                //取短信状态（state）
                state = Recv.Substring(len, 1);


                //跳过短信参数信息（如ID，状态）
                Recv = Recv.Substring(len);
                len = Recv.IndexOf("\r\n");
                //跳过回车换行
                len += 2;
                #endregion

                #region 解析PDU码
                #region PDU码中心服务地址
                //长度
                temp = Recv.Substring(len, 2);
                invel = TypeConvert.Str_2toInt(temp);
                len += 2;

                if (invel > 0)
                {
                    //地址类型
                    temp = Recv.Substring(len, 2);
                    mPDU.SCAType = ParseAdressType(temp);
                    len += 2;
                    //中心地址
                    test = invel * 2 - 2;
                    temp = Recv.Substring(len, test);
                    mPDU.SCAdress = UnInvertStr(temp);
                    len += test;
                }
                #endregion

                #region PDU类型
                temp = Recv.Substring(len, 2);
                mPDU.PDUType = ParsePDUType(temp);
                len += 2;
                #endregion

                #region 发送方地址
                //长度
                temp = Recv.Substring(len, 2);
                invel = TypeConvert.Str_2toInt(temp);
                //如果长度为奇数则加1
                if (invel % 2 == 1)
                {
                    invel += 1;
                }
                len += 2;

                if (invel > 0)
                {
                    //地址类型
                    temp = Recv.Substring(len, 2);
                    mPDU.OAType = ParseAdressType(temp);
                    len += 2;
                    //中心地址
                    temp = Recv.Substring(len, invel);
                    mPDU.OAdress = UnInvertStr(temp);
                    len += test;
                }
                #endregion

                #region 协议标识
                temp = Recv.Substring(len, 2);
                test = TypeConvert.Str_2toInt(temp);
                mPDU.ProtocolIdentifier = (EProtocolIdentifier)test;
                len += 2;
                #endregion

                #region 编码方式
                temp = Recv.Substring(len, 2);
                mPDU.DataCodingScheme = ParseCodeScheme(temp);
                len += 2;
                #endregion

                #region 服务中心收到短信的时间
                temp = Recv.Substring(len, 12);
                temp = UnInvertStr(temp);
                temp = temp.Substring(0, 2) + "/" + temp.Substring(2, 2) + "/"
                     + temp.Substring(4, 2) + " " + temp.Substring(6, 2) + ":"
                     + temp.Substring(8, 2) + ":" + temp.Substring(10, 2);
                mPDU.SCTS = Convert.ToDateTime(temp);
                len += 14;
                #endregion

                #region 短信内容
                Recv = Recv.Substring(len);
                len = 0;
                //解析短息内容
                if (mPDU.DataCodingScheme.CodeScheme == ECodeScheme.Unicode)
                {
                    //长度
                    temp = Recv.Substring(len, 2);
                    invel = TypeConvert.Str_2toInt(temp);
                    len += 2;
                    //短信内容
                    test = invel * 2;
                    temp = Recv.Substring(len, test);

                    mPDU.UserData = ConvertFromUnicode(temp);
                }
                else if (mPDU.DataCodingScheme.CodeScheme == ECodeScheme.Sbit)
                {
                    //长度
                    temp = Recv.Substring(len, 2);
                    invel = TypeConvert.Str_2toInt(temp);
                    len += 2;

                    //短信内容
                    test = Recv.IndexOf("\r");
                    temp = Recv.Substring(len, (test - len));

                    mPDU.UserData = ConvertFrom7bit(temp);
                }
                Recv = Recv.Substring(test);
                #endregion
                #endregion
                #endregion
            }
            return mPDU;
        }
        /// <summary>
        /// 将从模块中通过命令AT+CMGR接收到的短信解析,并以【STSMS】类型返回
        /// </summary>
        /// <param name="Recv"></param>
        /// <returns></returns>
        public STSMS ParsePDU_CMGR(string Recv, bool b)
        {
            //获取AT+CMGL列出的短信
            int len;
            int invel = 0;
            string temp = "";
            int test = 0;
            string state = "";//短信状态
            STPDUParam mPDU = new STPDUParam();
            STSMS mSMS = new STSMS();
            len = Recv.IndexOf("+CMGR:");

            if (len != 0)
            {
                #region 解析CMGR命令返回
                #region AT+CMGL=2的回传信息
                //AT ＋ CMGR=2 { 读未发短信息 } 
                //＋CMGR:（空格） 0,,24 {1 表示短信ID， 2 表示未发信息， 24 表示短信长度 }（回车，换行） pdu
                //0891683108200505F0  11  0B813106656930F1  0000  40015280353500 04 D4F29C0E （回车换行回车换行）
                //OK （回车换行）
                #endregion

                #region 解析短信参数信息（如ID，状态）
                //跳过+CMGR:           
                len += 6;

                //跳过空格
                len += 1;

                //取消息状态（state）
                state = Recv.Substring(len, 1);

                //跳过短信参数信息（如状态，长度）
                Recv = Recv.Substring(len);
                len = Recv.IndexOf("\r\n");
                //跳过回车换行
                len += 2;
                #endregion

                #region 解析PDU码
                #region PDU码中心服务地址
                //长度
                temp = Recv.Substring(len, 2);
                invel = TypeConvert.Str_2toInt(temp);
                len += 2;

                if (invel > 0)
                {
                    //地址类型
                    temp = Recv.Substring(len, 2);
                    mPDU.SCAType = ParseAdressType(temp);
                    len += 2;
                    //中心地址
                    test = invel * 2 - 2;
                    temp = Recv.Substring(len, test);
                    mPDU.SCAdress = UnInvertStr(temp);
                    len += test;
                }
                #endregion

                #region PDU类型
                temp = Recv.Substring(len, 2);
                mPDU.PDUType = ParsePDUType(temp);
                len += 2;
                #endregion

                #region 发送方地址
                //长度
                temp = Recv.Substring(len, 2);
                invel = TypeConvert.Str_2toInt(temp);
                //如果长度为奇数则加1
                if (invel % 2 == 1)
                {
                    invel += 1;
                }
                len += 2;

                if (invel > 0)
                {
                    //地址类型
                    temp = Recv.Substring(len, 2);
                    mPDU.OAType = ParseAdressType(temp);
                    len += 2;
                    //中心地址
                    temp = Recv.Substring(len, invel);
                    mPDU.OAdress = UnInvertStr(temp);
                    len += test;
                }
                #endregion

                #region 协议标识
                temp = Recv.Substring(len, 2);
                test = TypeConvert.Str_2toInt(temp);
                mPDU.ProtocolIdentifier = (EProtocolIdentifier)test;
                len += 2;
                #endregion

                #region 编码方式
                temp = Recv.Substring(len, 2);
                mPDU.DataCodingScheme = ParseCodeScheme(temp);
                len += 2;
                #endregion

                #region 服务中心收到短信的时间
                temp = Recv.Substring(len, 12);
                temp = UnInvertStr(temp);
                temp = temp.Substring(0, 2) + "/" + temp.Substring(2, 2) + "/"
                     + temp.Substring(4, 2) + " " + temp.Substring(6, 2) + ":"
                     + temp.Substring(8, 2) + ":" + temp.Substring(10, 2);
                mPDU.SCTS = Convert.ToDateTime(temp);
                len += 14;
                #endregion

                #region 短信内容
                Recv = Recv.Substring(len);
                len = 0;
                //解析短息内容
                if (mPDU.DataCodingScheme.CodeScheme == ECodeScheme.Unicode)
                {
                    //长度
                    temp = Recv.Substring(len, 2);
                    invel = TypeConvert.Str_2toInt(temp);
                    len += 2;
                    //短信内容
                    test = invel * 2;
                    temp = Recv.Substring(len, test);

                    mPDU.UserData = ConvertFromUnicode(temp);
                }
                else if (mPDU.DataCodingScheme.CodeScheme == ECodeScheme.Sbit)
                {
                    //长度
                    temp = Recv.Substring(len, 2);
                    invel = TypeConvert.Str_2toInt(temp);
                    len += 2;

                    //短信内容
                    test = Recv.IndexOf("\r");
                    temp = Recv.Substring(len, (test - len));

                    mPDU.UserData = ConvertFrom7bit(temp);
                }
                Recv = Recv.Substring(test);
                #endregion
                #endregion
                #endregion

                mSMS.PHONE = mPDU.OAdress;
                mSMS.SMS = mPDU.UserData;
            }
            return mSMS;
        }

        #endregion

        #region PDU码相关转换函数
        #region Unicode编码
        /// <summary>
        /// 将字符串转换为Unicode码
        /// </summary>
        /// <param name="smsg"></param>
        /// <returns></returns>
        private string ConvertToUnicode(string smsg)
        {
            string res = "";
            string ls;
            string rs;
            byte[] resByte = System.Text.UnicodeEncoding.Unicode.GetBytes(smsg);
            for (int i = 0; i < resByte.Length; i += 2)
            {
                ls = resByte[i].ToString("X2");//将一个字节16进制数转换为2位字符，如0xab-〉AB
                rs = resByte[i + 1].ToString("X2");
                res = res + rs + ls; //注意这里高低位颠倒
            }
            return res;

        }
        /// <summary>
        /// 将Unicode码字符转为对应的内容
        /// </summary>
        /// <param name="usc"></param>
        /// <returns></returns>
        private string ConvertFromUnicode(string usc)
        {
            string res = "";
            int len = usc.Length;
            byte test;
            byte[] resByte = new byte[len / 2];
            for (int i = 0, j = 0; i < len; i += 2, j++)
            {
                resByte[j] = TypeConvert.Str_2toByte(usc.Substring(i, 2));
            }
            for (int p = 0; p < resByte.Length; p += 2)
            {
                test = resByte[p];
                resByte[p] = resByte[p + 1];
                resByte[p + 1] = test;
            }
            res = System.Text.UnicodeEncoding.Unicode.GetString(resByte);
            return res;
        }
        #endregion

        #region 字符串反转
        /// <summary>
        /// 将手机号中的字符两两反转，如果是奇数个字符，则在最末加F
        /// </summary>
        /// <param name="MobileNum"></param>
        /// <returns></returns>
        private string InvertPhone(string MobileNum)
        {
            string ltem, rtem, ttem;
            int tl;//手机号长度
            int i = 0;
            ttem = "";//转换后的字符串
            tl = MobileNum.Trim().Length;
            if ((tl != 11) && (tl != 13))
            {
                return "";
            }
            if (tl == 11)  // 11位转换为13位
            {
                tl += 2;
                MobileNum = "86" + MobileNum;
            }

            for (i = 0; i < tl; i += 2)
            {
                ltem = MobileNum.Substring(i, 1);
                if (tl % 2 == 0)//偶数
                {
                    rtem = MobileNum.Substring(i + 1, 1);
                }
                else
                {
                    if (i == (tl - 1))
                    {
                        rtem = "F";
                    }
                    else
                    {
                        rtem = MobileNum.Substring(i + 1, 1);
                    }
                }

                ttem += rtem + ltem;  //每两位颠倒
            }
            return ttem;

        }
        /// <summary>
        /// 将字符串中的字符两两反转，如果是奇数个字符，则在最末加F
        /// </summary>
        /// <param name="temp"></param>
        /// <returns></returns>
        private string InvertStr(string temp)
        {
            string ltem, rtem, ttem;
            int tl;//手机号长度
            int i = 0;
            ttem = "";//转换后的字符串
            tl = temp.Length;

            for (i = 0; i < tl; i += 2)
            {
                ltem = temp.Substring(i, 1);
                if (tl % 2 == 0)//偶数
                {
                    rtem = temp.Substring(i + 1, 1);
                }
                else
                {
                    if (i == (tl - 1))
                    {
                        rtem = "F";
                    }
                    else
                    {
                        rtem = temp.Substring(i + 1, 1);
                    }
                }

                ttem += rtem + ltem;  //每两位颠倒
            }
            return ttem;
        }
        /// <summary>
        /// 对字符串进行两两反转，如果末尾是F，则去掉
        /// </summary>
        /// <param name="temp"></param>
        /// <returns></returns>
        private string UnInvertStr(string temp)
        {
            string str = "";
            string ltem, rtem, ttem = "";
            int len;
            len = temp.Length;
            for (int i = 0; i < len; i += 2)
            {
                ltem = temp.Substring(i, 1);
                rtem = temp.Substring(i + 1, 1);
                ttem += rtem + ltem;
            }

            str = ttem.Substring(len - 1, 1);
            if (str == "F")
            {
                ttem = ttem.Remove(len - 1);
            }
            return ttem;
        }
        #endregion

        #region 7位编码
        /// <summary>
        /// 将ASCII码字符串转换为7-bit编码的字符串
        /// </summary>
        /// <param name="smsg">待转换ASCII码字符串</param>
        /// <returns></returns>
        private string ConvertTo7bit(string smsg)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            byte retain = 0;    // 上一字节残余的数据
            byte handle = 0;//缓冲当前处理的数据
            byte temp = 0;
            int len; //待编码字符串长度

            for (int i = 0; i < smsg.Length; i++)
            {

                // 处理源串的每个字节
                len = i & 7;

                if (len == 0)//此时表示第一个位（0）
                {
                    // 组内第一个字节，只是保存起来，待处理下一个字节时使用
                    retain = Convert.ToByte(Convert.ToChar(smsg.Substring(i, 1)));
                }
                else
                {
                    handle = Convert.ToByte(Convert.ToChar(smsg.Substring(i, 1)));
                    temp = Convert.ToByte(((handle << (8 - len)) | retain) & 0xff);
                    sb.Append(temp.ToString("X2"));

                    // 将该字节剩下的左边部分，作为残余数据保存起来
                    retain = Convert.ToByte(handle >> len);//右移即去掉补充到前面字节的部分
                    // 修改目标串的指针和计数值 pDst++;
                }

            }
            if (smsg.Length % 8 != 0)
            {
                sb.Append(retain.ToString("X2"));
            }
            return sb.ToString(); ;

        }
        /// <summary>
        /// 将7-bit编码字符串转换为ASCII码字符串
        /// </summary>
        /// <param name="s">待转换7-bit编码字符串</param>
        /// <returns></returns>
        private string ConvertFrom7bit(string s)
        {
            int iByte = 0;//字节位置（个数）
            int iLeft = 0;//缓冲多余的字节位数

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int i = 0; i < s.Length; i += 2)
            {
                byte bSrc = byte.Parse(s.Substring(i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                //   将源字节右边部分与残余数据相加，去掉最高位，得到一个目标解码字节   
                sb.Append(Convert.ToChar((((bSrc << iByte) | iLeft) & 0x7f)));
                //   将该字节剩下的左边部分，作为残余数据保存起来   
                iLeft = bSrc >> (7 - iByte);
                //   修改字节计数值   
                iByte++;
                //   到了一组的最后一个字节   
                if (iByte == 7)
                {
                    //   额外得到一个目标解码字节   
                    sb.Append(Convert.ToChar(iLeft));
                    //   组内字节序号和残余数据初始化   
                    iByte = 0;
                    iLeft = 0;
                }
            }
            return sb.ToString();
        }
        #endregion

        #endregion

        #endregion
    }
}
